<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/11
 * Time: 10:54
 */
namespace Service\Controller;
use Admin\Controller\AdminController;

class CenterController extends AdminController
{
    public function index(){
        $req = I('param.');
        $tel = $req['tel'];
        $data_tel = D('Customer')->where(array('tel'=>$tel))->relation(array('families','orders'))->select();
        $data_contact = D('Customer')->where(array('contact'=>array('like','%'.$tel.'%')))->relation(array('families','orders'))->limit(10)->select();
        if($data_tel){
            $data_tel[0]['primary'] = 1;
        }
        $data = array_merge($data_tel,$data_contact);
//        $data = D('Customer')->relation(true)->select();
        $count['ing'] = M('Order')->where(array('BEGIN_USER'=>getUserId(),'TO_USER'=>''))->count();
        $count['ed'] = M('Order')->where(array('BEGIN_USER'=>getUserId(),'TO_USER'=>array('neq','')))->count();

        $this->assign('data',$data);
        $this->assign('user',getUser());
        $this->assign('count',$count);
//        dump($data[0]['orders']);
        $this->display();
    }

    public function choice(){
        $req = I('param.');
        if($req['tel']){
            $tel = $req['tel'];
            $contact = array('like','%'.$req['tel'].'%');
            $where['tel'] = $tel;
            $where['contact'] = $contact;
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        if($req['name']){
            $name = $req['name'];
            $map['name'] = $name;
        }

        $customer = D('Customer')->where($map)->relation(array('families'))->select();
        $this->assign('customer',$customer);
        $this->assign('user',getUser());
//        dump($customer);
        $this->display();
    }

    public function getCustomer(){
        $req = I('param.');
        if($req['id'] == 'all'){
            $data = D('Admin/Customer')->relation(true)->select();
            $count = D('Admin/Customer')->count();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到'.$count.'条数据' : '没有找到数据';
            $res['data'] = $data;
        }elseif($req['id']){
            $where['id'] = $req['id'];
            $data = D('Admin/Customer')->where($where)->relation(true)->find();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到1条数据' : '没有找到数据';
            $res['data'] = $data;
        }else{
            $res['stat'] = 0;
            $res['msg'] = '参数提交不正确';
        }
        foreach(D('Admin/Dict')->getDict('ugroup') as $k=>$v){
            $ugroup[$v['value']] = $v;
        }
        $this->assign('data',$res['data']);
        $this->assign('ugroup',$ugroup);
        // dump($ugroup);
        // dump($data);
        $this->display();
    }

    public function edit(){
        $customer_id = I('param.')['id'];
        $customer = D('Admin/Customer')->relation(true)->find($customer_id);
        $this->assign('customer',$customer);
        $this->display();
    }
}